package cn.net.wangyixinwen.mvvm.vm;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.net.wangyixinwen.api.ApiRepository;
import cn.net.wangyixinwen.bean.NewsBean;
import cn.net.wangyixinwen.event.SingleLiveEvent;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.observers.DisposableObserver;

public class JXVM extends BaseViewModel {

    public SingleLiveEvent<List<NewsBean>> eventList = new SingleLiveEvent<>();

    public void getData() {
        ApiRepository.getJX()
                .safeSubscribe(new DisposableObserver<Map<String, List<NewsBean>>>() {
                    @Override
                    public void onNext(@NonNull Map<String, List<NewsBean>> map) {
                        List<NewsBean> list = new ArrayList<>();
                        for (String key : map.keySet()) {
                            list.addAll(map.get(key));
                        }
                        eventList.setValue(list);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        ToastUtils.showShort("网络请求失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
